$(function () {
    $.ajax({
        type: 'POST',
        url: '/cancellation/getSettleMerchantMsg',
        async: false,
        data: {},
        dataType: 'json',
        success: function (result) {
            if (result.code==666){
                var merchantCount = result.data.merchantList.length;
                var count = 0;
                for (i = 0;i<merchantCount;i++) {
                    $("#able-merchant").append("<div class='able-a-merchant'><a class='able-a-merchant ' onclick='selectMerchant("+result.data.merchantList[i].id+");'>"+result.data.merchantList[i].name+"</a></div>");

                }
                selectMerchant(result.data.merchantList[0].id)
            }else{
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status==555){
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                        top.location.href="/login";
                    });
                });
            }else{
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('页面加载失败',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                    });
                });
            }
        },
    });
});
function selectMerchant(id) {
    $("#account_table").empty();
    $.ajax({
        type: 'POST',
        url: '/enable/merchantDetail',
        async: false,
        data: {"merchantId":id},
        dataType: 'json',
        success: function (result) {
            if (result.code==666){
                $("input[name='name']").val(result.data.merchant.name);
                $("input[name='id']").val(result.data.merchant.id);
                $("input[name='code']").val(result.data.merchant.code);
                $("input[name='wallet']").val("预付费子账户钱包");
                $("input[name='p1']").val(result.data.merchant.p1);
                $("input[name='phone']").val(result.data.merchant.phone1);
                $("input[name='phone1']").val(result.data.merchant.phone1);
                $("input[name='mail1']").val(result.data.merchant.mail1);
                $("input[name='addr1']").val(result.data.merchant.addr1);
                $("input[name='p2']").val(result.data.merchant.p2);
                $("input[name='phone2']").val(result.data.merchant.phone2);
                $("input[name='mail2']").val(result.data.merchant.mail2);
                $("input[name='addr2']").val(result.data.merchant.addr2);
                var accountlength =result.data.accountList.length;
                if (accountlength>0){
                    for (var i=0;i<accountlength;i++){
                        var power_type = gettype(result.data.accountList[i].powertype);
                        var charge = gettype(result.data.accountList[i].charge);
                        var accountStr ="<div class=\"col-md-3 col-msg\"style=\"max-width: 20%\">\n" +
                            "                                    <input type=\"text\" placeholder=\"\" maxlength=\"2000\" style=\"width: 112%;\"\n" +
                            "                                            value=\""+result.data.accountList[i].name+"\" class=\"form-control msg-val\" />\n" +
                            "                                </div>\n" +
                            "                                <div class=\"col-md-3 col-msg\" style=\"max-width: 20%\">\n" +
                            "                                    <input type=\"text\" placeholder=\"\" maxlength=\"2000\" style=\"width: 112%;\"\n" +
                            "                                            value=\""+power_type+"\" class=\"form-control msg-val\" />\n" +
                            "                                </div>\n" +
                            "                                <div class=\"col-md-3 col-msg\" style=\"max-width: 20%\">\n" +
                            "                                    <input type=\"text\" placeholder=\"\" maxlength=\"2000\" style=\"width: 112%;\"\n" +
                            "                                            value=\""+result.data.accountList[i].collectors+"\" class=\"form-control msg-val\" />\n" +
                            "                                </div>\n" +
                            "                                <div class=\"col-md-3 col-msg\"  style=\"max-width: 20%\">\n" +
                            "                                    <input type=\"text\" placeholder=\"\" maxlength=\"2000\" style=\"width: 112%;\"\n" +
                            "                                           value=\""+result.data.accountList[i].readingString+"\" class=\"form-control msg-val\" />\n" +
                            "                                </div>\n" +
                            "                                <div class=\"col-md-3 col-msg\" style=\"max-width: 20%\" >\n" +
                            "                                    <input type=\"text\" placeholder=\"\" maxlength=\"2000\" style=\"width: 112%;\"\n" +
                            "                                           value=\""+charge+"费\" class=\"form-control msg-val\" />\n" +
                            "                                </div>"
                        $("#account_table").append(accountStr);
                    }
                }
            }else{
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status==555){
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                        top.location.href="/login";
                    });
                });
            }else{
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('页面加载失败',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                    });
                });
            }
        },
    });
}
function  gettype(str) {
    if (str==0){
        return "电";
    }else if (str==1) {
        return "水";
    }else{
        return "气";
    }
}
function modifyMerchant() {
    var name = $("input[name='name']").val();
    var id = $("input[name='id']").val();
    var p1 = $("input[name='p1']").val();
    var phone1 =$("input[name='phone1']").val();
    var mail1 =$("input[name='mail1']").val();
    var addr1 =$("input[name='addr1']").val();
    var p2 = $("input[name='p2']").val();
    var phone2 = $("input[name='phone2']").val();
    var mail2 = $("input[name='mail2']").val();
    var addr2 = $("input[name='addr2']").val();
    $.ajax({
        type: 'POST',
        url: '/enable/modifyMerchant',
        async: false,
        data: {"name":name,"id":id,"p1":p1,"phone1":phone1,"mail1":mail1,"addr1":addr1,"p2":p2,"phone2":phone2,"mail2":mail2,"addr2":addr2,},
        dataType: 'json',
        success: function (result) {
            if (result.code==666){
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('保存成功',{icon:1}, function(index){
                        layer.close(index);
                        selectMerchant(id);
                    });
                });
            }else{
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status==555){
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                        top.location.href="/login";
                    });
                });
            }else{
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('页面加载失败',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                    });
                });
            }
        },
    });
}

$("#enable").click(function () {
    var layer;
    layui.use(['layer', 'form'],function() {
        layer = layui.layer;
        layer.confirm("确认要销户吗？", {
            btn: ['确定', '取消'] //按钮
        }, function () {

            layer.close(layer.index);
            var id = $("input[name='id']").val();
            $.ajax({
                type: 'POST',
                url: '/cancellation/pinHouseholds',
                async: false,
                data: {"merchantId": id},
                dataType: 'json',
                success: function (result) {
                    if (result.code == 666) {
                        var layer;
                        layui.use(['layer', 'form'], function () {
                            layer = layui.layer
                            layer.alert('销户成功', {icon: 1}, function (index) {
                                //do something
                                layer.close(index);
                                location.reload(false);
                            });
                        });
                    } else {
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    if (XMLHttpRequest.status==555){
                        var layer;
                        layui.use(['layer', 'form'], function(){
                            layer = layui.layer
                            layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                                //do something
                                layer.close(index);
                                top.location.href="/login";
                            });
                        });
                    }else{
                        var layer;
                        layui.use(['layer', 'form'], function(){
                            layer = layui.layer
                            layer.alert('页面加载失败',{icon:7}, function(index){
                                //do something
                                layer.close(index);
                            });
                        });
                    }
                },
            });
        }, function () {
        });
    });
});